//
//  VisitorView.swift
//  HMWeibo
//
//  Created by wulei on 15/12/6.
//  Copyright © 2015年 wulei. All rights reserved.
//

import UIKit
import SnapKit

//protocol  VisitorViewDelegate: NSObjectProtocol {
///// 注册
//    func visitorViewDidRegister()
//    
///// 登录
//    func visitorViewDidLogin()
//}


class VisitorView: UIView {
    
//    // 设置代理协议
//    weak var delegate: VisitorViewDelegate?
//    
//    @objc func clickRegist() {
//        
//        delegate?.visitorViewDidRegister()
//        
//    }
//    
//    @objc func clickLogin() {
//        
//        delegate?.visitorViewDidLogin()
//        
//    }
    
    
    
// MARK: - 设置视图信息
    func setupViewInfo(imageName: String? ,title: String) {
        messageLabel.text = title
        
        guard let imgName = imageName else{
            startAnim()
            return
        }
        
        iconView.image = UIImage(named: imgName)
        homeIconView.hidden = true
        sendSubviewToBack(maskIconView)
    }
    
    func startAnim() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        anim.removedOnCompletion = false
        
        iconView.layer .addAnimation(anim, forKey: nil)
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupUI()
    }

    // 图标
   private lazy var iconView: UIImageView = UIImageView(imageName:  "visitordiscover_feed_image_smallicon")
    
    // 遮罩
    private lazy var maskIconView: UIImageView = UIImageView(imageName:  "visitordiscover_feed_mask_smallicon")
    
    // 小房子
    private lazy var homeIconView: UIImageView = UIImageView(imageName:  "visitordiscover_feed_image_house")
    
    // 消息文字
    private lazy var messageLabel: UILabel = UILabel(title: "关注一些人,回这里看看有什么惊喜")

    
    // 注册按钮
lazy var registerButton: UIButton = UIButton(title: "注册", color: UIColor.orangeColor(), backimageName: "common_button_white_disable")

    // 登陆按钮
lazy var loginButton: UIButton = UIButton(title: "登录", color: UIColor.darkGrayColor(), backimageName: "common_button_white_disable")

    
}

extension VisitorView {
/// 设置界面
    func setupUI() {
/// 添加子控件
        
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
/// 设置自动布局

        
        // 图标
        // make 理解为要添加的约束对象
        iconView.snp_makeConstraints { (make) -> Void in
            
        // 指定centerX属性 等于   参照对象 snp_参照属性值
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
            
        }
        
        // 小房子
        // centerX + centerY positions
        homeIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView.snp_center)
        }
        

        // 消息文字
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
            make.width.equalTo(224)
            make.height.equalTo(36)
        }
        

        
        // 注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }

        // 登录按钮
       loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        

        //        遮罩图像
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
            make.left.equalTo(self.snp_left)
            make.bottom.equalTo(loginButton.snp_bottom)
        }
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
    }
    
    
    
}